package com.linkoog.devtools.code_generator.util;

import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiFileFactory;
import com.intellij.psi.PsiJavaFile;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
import org.apache.commons.io.FilenameUtils;
import org.jetbrains.annotations.NotNull;

import java.util.*;

public class PsiFileUtil {

    public static void createFile(Project project, PsiDirectory basePsiDirectory, @NotNull Collection<CreateFileParam> createFileParams){
        CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(project);
        JavaCodeStyleManager javaCodeStyleManager = JavaCodeStyleManager.getInstance(project);

        WriteCommandAction.runWriteCommandAction(project, () -> {
            try {
                Map<PsiFile, PsiDirectory> newPsiFiles = new HashMap<>();
                for (CreateFileParam createFileParam : createFileParams) {
                    String extension = FilenameUtils.getExtension(createFileParam.fullFileName);
                    FileType fileType = FileTypeUtil.getFileType(extension);
                    PsiDirectory psiDirectory = PsiDirectoryUtil.createSubDirectory(basePsiDirectory, createFileParam.subPath);
                    VirtualFile targetFile = psiDirectory.getVirtualFile().findChild(createFileParam.fullFileName);
                    if (targetFile == null){
                        PsiFile psiFile = PsiFileFactory.getInstance(project).createFileFromText(createFileParam.fullFileName, fileType, createFileParam.context);
                        newPsiFiles.put(psiFile, psiDirectory);
                    } else {
                        // 文件已存在
                        // Messages.showMessageDialog(String.format("文件已存在%s", fullFileName), "错误提示", PlatformIcons.ERROR_INTRODUCTION_ICON);
                        targetFile.setBinaryContent(createFileParam.context.getBytes());
                    }
                }

                for (PsiFile psiFile : newPsiFiles.keySet()) {
                    PsiDirectory psiDirectory = newPsiFiles.get(psiFile);
                    psiDirectory.add(psiFile);
                    codeStyleManager.reformat(psiFile);
                    if (psiFile instanceof PsiJavaFile) {
                        javaCodeStyleManager.optimizeImports(psiFile);
                        javaCodeStyleManager.shortenClassReferences(psiFile);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    public static boolean createFile(Project project, PsiDirectory basePsiDirectory, @NotNull CreateFileParam createFileParam){
        String extension = FilenameUtils.getExtension(createFileParam.fullFileName);
        FileType fileType = FileTypeUtil.getFileType(extension);

        return WriteCommandAction.runWriteCommandAction(project, (Computable<Boolean>)() -> {
            try {
                PsiDirectory psiDirectory = null;
                if (StringUtil.isNotBlank(createFileParam.subPath)){
                    psiDirectory = PsiDirectoryUtil.createSubDirectory(basePsiDirectory, createFileParam.subPath);
                } else {
                    psiDirectory = basePsiDirectory;
                }

                VirtualFile targetFile = psiDirectory.getVirtualFile().findChild(createFileParam.fullFileName);
                if (targetFile == null){
                    PsiFile psiFile = PsiFileFactory.getInstance(project).createFileFromText(createFileParam.fullFileName, fileType, createFileParam.context);
                    psiDirectory.add(psiFile);
                    CodeStyleManager.getInstance(project).reformat(psiFile);
                    if (psiFile instanceof PsiJavaFile) {
                        JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(project);
                        styleManager.optimizeImports(psiFile);
                        styleManager.shortenClassReferences(psiFile);
                    }
                } else {
                    // 文件已存在
                    // Messages.showMessageDialog(String.format("文件已存在%s", fullFileName), "错误提示", PlatformIcons.ERROR_INTRODUCTION_ICON);
                    targetFile.setBinaryContent(createFileParam.context.getBytes());
                }
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        });
    }



    public static class CreateFileParam{
        String subPath;
        String fullFileName;
        String context;

        public CreateFileParam() {
        }

        public CreateFileParam(String subPath, String fullFileName, String context) {
            this.subPath = subPath;
            this.fullFileName = fullFileName;
            this.context = context;
        }

        public String getSubPath() {
            return subPath;
        }

        public void setSubPath(String subPath) {
            this.subPath = subPath;
        }

        public String getFullFileName() {
            return fullFileName;
        }

        public void setFullFileName(String fullFileName) {
            this.fullFileName = fullFileName;
        }

        public String getContext() {
            return context;
        }

        public void setContext(String context) {
            this.context = context;
        }
    }


}
